<template>
    <div>
        <h1>课程添加</h1>

        <van-cell-group>
            <van-field v-model="course_name" label="课程" placeholder="请输入课程名称" />

            <van-field v-model="price" label="价格" placeholder="请输入课程价格" />
        </van-cell-group>
        
        <van-cell-group>

            <van-uploader :after-read="afterRead" />

        </van-cell-group>


        <!-- 视频控件 -->
        <van-cell-group>

            <video width="300" height="240" :src="src" controls="controls" autoplay="autoplay" />

        </van-cell-group>

        <van-button type="primary" @click="buy">确认添加</van-button>

    </div>
</template>

<script>
import mycate from './mycate'
export default {
    data() {
        return {
            data:{},
            pid:"",
            src:"",
            course_name:'',
            price:'',
            video:'',
            uid:localStorage.getItem('uid'),
            token:localStorage.getItem('token')
        }
    },

    components:{
        mycate
    },

    mounted:function(){
        
        // 接收参数
        this.pid = this.$route.query.pid
        console.log(this.pid)
    },

    methods:{
        get_data:function(){
            
            console.log(res);

                var mytree = {'id':0, name:""}

                mytree['children'] = res.data

                this.data = mytree
        },

        // 文件上传   file: 文件实体
        afterRead:function(file){

            // 定义参数
            let data = new FormData()
            data.append('file',file.file)

            // 创建实例
            const axiosInstance = this.axios.create({withCredentials:false})

            axiosInstance({method:'POST',url:'http://localhost:8000/uploader/',data:data}).then(res=>{
                console.log(res)
                if (res.status==200){
                    this.$toast('上传成功')
                    this.src = "http://localhost:8000/static/upload/" + res.data.filename;
                    this.video = res.data.filename
                }

            })
        },

        buy:function(){

            this.axios.post('http://localhost:8000/courseadd/',this.qs.stringify({'course_name':this.course_name,
            'price':this.price,'uid':this.uid,'cate_id':this.pid,'video':this.video,'token':this.token})).then(res=>{
                console.log(res.data)

                if (res.data.code == 200){
                    this.$toast('课程添加成功')
                }else{
                    this.$toast('课程添加失败')
                }

            })
        }
    }

}
</script>

<style>

</style>